<template>
  <view class="cart">
    <view class="top">
      <view>30天无忧退货</view>
      <view>48小时快速退款</view>
      <view>满88元免邮费</view>
    </view>
    <view class="cartlist">
      <view class="item" v-for="(item, index) in listData" :key="index">
        <view class="con">
          <view class="left">
            <view class="icon" @click="changeColor(index, item.goods_id)" :class="[Listids[index] ? 'active' : '']"></view>
            <view class="img">
              <image :src="item.list_pic_url" alt="">
            </view>
            <view class="info">
              <view>{{item.goods_name}}</view>
              <view>¥{{item.retail_price}}</view>
            </view>
          </view>
          <view class="right">
            <view class="num">x {{item.number}}</view>
          </view>
        </view>
      </view>
    </view>

    <view class="fixed">
      <view class="left" @click="allCheck" :class="{'active': allcheck}">
        全选({{isCheckedNumber}})
      </view>
      <view class="right">
        <view>¥{{allPrice}}</view>
        <view @click="orderDown">下单</view>
      </view>
    </view>
  </view>
</template>

<script>
	export default {
		data() {
			return {
				openId: '',
				listData: [],
				Listids: [],
				allcheck: false
			}
		},
		onLoad() {
			this.openId = uni.getStorageSync('openId')
		},
		onShow() {
			this.getlistData()
		},
		computed:{
			isCheckedNumber(){
				let number = 0
				for(let i=0 ; i<=this.Listids.length ; i++){
					if(this.Listids[i]){
						number++
					}
				}
				if(number == this.listData.length && number !== 0){
					this.allcheck = true
				}else{
					this.allcheck = false
				}
				return number
			},
			allPrice(){
				let Price = 0
				for(let i = 0; i <= this.Listids.length; i++){
					if(this.Listids[i]){
						Price += this.listData[i].retail_price * this.listData[i].number
					}
				}
				return Price
			}
		},
		methods: {
			changeColor(index,id){
				if(this.Listids[index]){
					this.$set(this.Listids,index,false)
				}else{
					this.$set(this.Listids,index,id)
				}
			},
			allCheck(){
				this.Listids = []
				if(this.allcheck){
					this.allcheck = false
				}else{
					this.allcheck = true
					this.listData.forEach(item => {
						this.Listids.push(item.goods_id)
					})
				}
			},
			async orderDown(){
				if(this.Listids.length == 0){
					uni.showToast({
						title:"请选择商品",
						icon:"success",
						duration:1000,
						success(){}
					})
					return false
				}
				let newgoodsid = []
				for(let i = 0; i < this.Listids.length; i++){
					const element = this.Listids[i]
					if(element){
						newgoodsid.push(element)
					}
				}
				let goodsid = newgoodsid.join(',')
				const data = await this.$request({
					url:'/order/submitAction',
					method:'POST',
					data:{
						goodsId:goodsid,
						openId:this.openId,
						allPrice:this.allPrice
					}
				})
				if(data){
					uni.navigateTo({
						url:'../order/order'
					})
				}
			},
			async getlistData(){
				const data = await this.$request({
					url:'/cart/cartList',
					data:{
						openId:this.openId
					}
				})
				console.log(data)
				this.listData = data.data.data
			}
		}
	}
</script>

<style lang="scss">
.cart {
  overflow-x: hidden;
  .top {
    display: flex;
    justify-content: space-between;
    padding: 30rpx 20rpx;
    view {
      background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/servicePolicyRed-518d32d74b.png) 0 center no-repeat;
      background-size: 10rpx;
      padding-left: 15rpx;
      display: flex;
      align-items: center;
      font-size: 25rpx;
      color: #666;
    }
  }
  .cartlist {
    background: #fff;
    margin-bottom: 110rpx;
    .item {
      padding: 20rpx 0;
      border-bottom: 1rpx solid #f4f4f4;
      height: 166rpx;
      position: relative;
      .con {
        display: flex;
        align-items: center;
        justify-content: space-between;
        transition: all 300ms ease;
        .left {
          display: flex;
          align-items: center;
          width: 80%;
          .icon {
            height: 125rpx;
            width: 34rpx;
            background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/checkbox-0e09baa37e.png) no-repeat center center;
            background-size: 34rpx 34rpx;
            margin: 0 20rpx;
          }
          .icon.active {
            background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/checkbox-checked-822e54472a.png) no-repeat center center;
            background-size: 34rpx 34rpx;
          }
          .img {
            height: 125rpx;
            width: 125rpx;
            display: block;
            background: #f4f4f4;
            image {
              width: 100%;
              height: 100%;
            }
          }
          .info {
            width: 50%;
            padding: 20rpx;
            view {
              line-height: 40rpx;
            }
          }
        }
        .right {
          padding-right: 50rpx;
        }
      }
      .delete {
        position: absolute;
        width: 100rpx;
        top: 0;
        right: -100rpx;
        text-align: center;
        height: 100%;
        background: #b4282d;
        color: #fff;
        transition: all 200ms ease;
        display: flex;
        align-items: center;
        justify-content: center;
        view {
          color: #fff;
        }
      }
    }
  }
  .fixed {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 100rpx;
    line-height: 100rpx;
    width: 100%;
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      display: flex;
      align-items: center;
      background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/checkbox-0e09baa37e.png) no-repeat;
      background-size: 34rpx 34rpx;
      background-position: 20rpx;
      padding-left: 70rpx;
    }
    .active {
      background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/checkbox-checked-822e54472a.png) no-repeat;
      background-size: 34rpx 34rpx;
      background-position: 20rpx;
    }
    .right {
      display: flex;
      view:nth-child(1) {
        color: #b4282d;
        padding-right: 40rpx;
      }
      view:nth-child(2) {
        width: 200rpx;
        height: 100rpx;
        text-align: center;
        line-height: 100rpx;
        font-size: 29rpx;
        background: #b4282d;
        color: #fff;
      }
    }
  }
  .nogoods {
    margin-top: 200rpx;
    image {
      margin: 0 auto;
      display: block;
      width: 258rpx;
      height: 258rpx;
    }
  }
}
</style>
